package Thread.ReentrantLock;

import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

public class Main {
    public static void main(String[] args) throws InterruptedException {
        var c=new Counter();
        var t1=new Thread(()->{
            for (int i = 0; i < 1000; i++) {
                c.add(1);
            }
        });
        var t2=new Thread(()->{
            for (int i = 0; i < 1000; i++) {
                c.add(1);
            }
        });
        var t3=new Thread(()->{
            for (int i = 0; i < 1000; i++) {
                c.add(1);
            }
        });
        t1.start();
        t2.start();
        t3.start();
        t1.join();
        System.out.println(c.getCount());
    }
}

class Counter {
    private int count = 0;
    private final Lock lock = new ReentrantLock();

    public void add(int n) {
        lock.lock();
        try{
            count += n;
        }finally {
            lock.unlock();
        }
    }

    public int getCount() {
        return count;
    }
}
